<?php
//$moduleid = 30;
include '../../common.inc.php';
$dbMain   = new PDO('mysql:host=192.168.1.19;dbname=ixdcw', 'root', 'root');
$dbSource = new PDO('mysql:host=192.168.1.19;dbname=caiji', 'root', 'root');
$table    = 'video_fangche';
$ds       = $dbSource->query('select max(ID) as num from ' . $table);
$fo       = $ds->fetchObject();
$maxId      = intval($fo->num);
if (!isset($id)) {
	$id = 0;
} else {
	if ($id > $maxId) {
		die(date('Y-m-d H:i:s'));
	}
}
$sql     = "select * from {$table} where id > $id and yifa=0 limit 1";
$result  = $dbSource->query($sql);
$rs      = $result->fetchAll();
$setting = PCache::read('module-10.php');
/**
'ID' => string '68' (length=2)
'yicai' => string '1' (length=1)
'yifa' => string '0' (length=1)
'title' => string '以房车为主题原创歌曲--《开着房车游世界》' (length=59)
'img' => string '/UploadFiles/2013-06/admin/2013061619115076704.jpg' (length=50)
'youku' => string 'XNTcxNTY4NDQw' (length=13)
'PageUrl' => string 'http://www.519rv.com/Item/Show.asp?m=115&d=359' (length=46)
 */
foreach ($rs as $r) {
	$content    = $dbMain->quote(Post::htmlEmbed($r['youku']));
	$title      = $r['title'];
	$addtime = time() - rand(0, 86400*120);
	//thumb
	$orginal    = 'http://www.519rv.com/' . $r['img'];
	$remoteName = Picture::name(10, 1, File::ext($r['img']), $addtime);
	Picture::saveRemote($orginal, $remoteName);
	$url = Picture::url($remoteName);
	$thumb = Picture::thumb($url, $setting['thumb_width'], $setting['thumb_height']);
	$edittime = time();
	$hits = rand(30, 5000);
	$insertMain = "insert into xd_video (title, thumb, addtime, edittime, hits, status) values(
		'$title',
		'$thumb',
		'$addtime',
		'$edittime',
		'$hits',
		3
	)";
	$dbMain->query($insertMain);
	$insertId = $dbMain->lastInsertId();
	$insertData = "insert into xd_video_data values ($insertId, $content)";
	if (!$dbMain->query($insertData)) {
		exit($insertData);
	}
	var_dump($insertId);
	$id = $r['ID'];
	$dbSource->query(Sql::update($table, 'yifa=1', 'id='.$r['ID']));
//	exit($start);
}


echo '<meta http-equiv="refresh" content="0; url=' . $_SERVER['PHP_SELF'] . '?id=' . $id . '" />';